#!/usr/bin/env python
# encoding: utf-8
'''
@author: aliax
@license: (C) Copyright 2018-2020.
@contact: 1048327635@qq.com
@file: thread.py
@time: 2020/7/9 0009 20:22
@desc:
'''
import threading
from concurrent.futures.thread import ThreadPoolExecutor

from Proxy.check_proxy import Check_proxy
from Proxy.get_proxy import Get_proxy
from Config.config import max_check_thread_all
from Config.config import max_check_thread_useful


check = Check_proxy()
get = Get_proxy()
def check_proxy_thread():
    """
    cli调用，启动检测程序
    :return:
    """
    #检测线程
    thread_list_all = []
    #二次检测线程
    thread_list_useful = []
    thread_list_all.clear()
    thread_list_useful.clear()
    #max_check_thread为线程数量参数，在config中修改

    for i in range(max_check_thread_all):
        t1 = threading.Thread(target=check.check,args=('all',))
        thread_list_all.append(t1)
    for i in range(max_check_thread_useful):
        t2 = threading.Thread(target=check.check, args=('useful',))
        thread_list_useful.append(t2)
    for t in thread_list_all:
        t.start()
    for t in thread_list_useful:
        t.start()

def get_proxy_thread():
    """
    cli工具调用，启动爬取程序
    :return:
    """
    #死循环，一直采集数据，可以修改为调度使用
    while(1):
        #采集速度较快，一个网站使用一个线程
        with ThreadPoolExecutor(max_workers=20) as t:  # 创建一个最大容纳数量为20的线程池
            t.submit(get.get_proxy, "快代理")
            t.submit(get.get_proxy, "齐云代理")
            t.submit(get.get_proxy, "云代理")
            t.submit(get.get_proxy, "免费代理库")
            t.submit(get.get_proxy, "ProxyList")
            t.submit(get.get_proxy, "proxylists")
            t.submit(get.get_proxy, "开心代理[普匿]")
            t.submit(get.get_proxy, "开心代理[高匿]")
            t.submit(get.get_proxy, "蝶鸟代理")
            t.submit(get.get_proxy, "89代理")
            t.submit(get.get_proxy, "小幻代理")
            #t.submit(get.get_proxy, "89代理")
